package com.shaoyu.elasticsearch.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManagerFactory;

@Component
public class IDUtil {


    private static int startIndex = 0;

    private static int length = 800000;
    private static int currentIndex = startIndex;

    public static String getUUID(){
        return CommonUtil.getUUID();
    }

    public static int getIncrementId(){
        if(startIndex == 0){
            startIndex=BeanUtil.getBean(JdbcTemplate.class).queryForObject("select nextval('normal')",Integer.class);
        }
        if(currentIndex-startIndex>=length){
            currentIndex=startIndex=BeanUtil.getBean(JdbcTemplate.class).queryForObject("select nextval('normal')",Integer.class);
        }
        return currentIndex++;
    }
}
